package com.scedu.spring5.dao;

import com.scedu.spring5.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Repository
public class BookDaoImpl implements BookDao {
    //注入JDBC Template
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //添加的方法
    @Override
    public void add(Book book) {
        //1.创建sql语句
        String sql="insert into t_book values(?,?,?)";
        //2.可变参数 数组
        Object[] args={book.getBookId(), book.getBookname(), book.getBookstatus()};
        //3.调用方法实现
        int update = jdbcTemplate.update(sql,args );
        System.out.println(update);
    }

    @Override
    public void edit(Book book) {
        //1.创建sql语句
        String sql="update  t_book set bookname=?,bookstatus=? where book_id=?";
        //2.可变参数 数组
        Object[] args={ book.getBookname(), book.getBookstatus(),book.getBookId()};
        //3.调用方法实现
        int update = jdbcTemplate.update(sql,args );
        System.out.println(update);
    }

    @Override
    public void delete(Integer id) {
        //1.创建sql语句
        String sql="delete from t_book where book_id=?";
        //2.可变参数 数组
        Object[] args={id};
        //3.调用方法实现
        int update = jdbcTemplate.update(sql,args );
        System.out.println(update);
    }
    //查询表记录的方法
    @Override
    public Integer findCount() {
        //1.创建sql语句
        String sql="select count(*) from t_book ";
        //2.调用方法实现
        return  jdbcTemplate.queryForObject(sql,Integer.class);

    }
    //查询对象的方法
    @Override
    public Book findOneBookById(Integer id) {
        //1.创建sql语句
        String sql="select * from t_book where book_id=?";
        //2.生成Book类型的RowMapper
        BeanPropertyRowMapper<Book> bookBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Book.class);
        //3.可变参数 数组
        Object[] args={id};
        //3.调用方法实现  queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
        return  jdbcTemplate.queryForObject(sql,bookBeanPropertyRowMapper,args);

    }
    //按书籍状态查询返回 集合
    @Override
    public List<Book> findAll(String status) {
        //1.创建sql语句
        String sql="select * from t_book where bookstatus=?";
        //2.生成Book类型的RowMapper
        BeanPropertyRowMapper<Book> bookBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Book.class);
        //3.可变参数 数组
        Object[] args={status};
        //3.调用方法实现   query(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
        return  jdbcTemplate.query(sql,bookBeanPropertyRowMapper,args);

    }
    //批量添加书籍
    @Override
    public void batchAddBook(List<Object[]> batchArgs) {
        //1.创建sql语句
        String sql="insert into t_book values(?,?,?)";

        //2.调用方法实现   query(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println(Arrays.toString(ints));

    }
    //批量修改书籍信息
    @Override
    public void batchEditBook(List<Object[]> batchArgs) {
        //1.创建sql语句
        String sql="update  t_book set bookname=?,bookstatus=? where book_id=?";
        //2.调用方法实现   query(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println(Arrays.toString(ints));
    }
    //批量删除书籍
    @Override
    public void batchDelBook(List<Object[]> batchArgs) {
        //1.创建sql语句
        String sql="delete from t_book where book_id=?";
        //2.调用方法实现   query(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println(Arrays.toString(ints));
    }
}
